package com.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author Yadasu
 * @date 2020/11/16 21:58
 */
public class TimeUtils {
    /**
     * 小时差
     * @param start 开始时间
     * @param end   结束时间
     * @return
     */
    public static Long getTheDifference(LocalDateTime start, LocalDateTime end) {
        Duration differenceValue = Duration.between(start ,end );
        return differenceValue.toHours();
    }

    /**
     * 时间格式转换
     * @param time
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date time){
        Instant it = time.toInstant();
        ZoneId zid = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(it, zid);
    }

    /**
     * 将收到的日期第一次格式化
     * @param date
     * @return
     */
    public static Date getDate(String date){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date parse = sdf.parse(date);
            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

}
